package com.cddata;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.border.BevelBorder;

import com.cddata.util.DBOperation;

@SuppressWarnings("serial")
public class Menu extends JPanel implements ActionListener{
	public Menu(){
		display();
	}
	
	public void display() {
		setLayout(new BoxLayout(this,BoxLayout.Y_AXIS));
		setBorder(new BevelBorder(BevelBorder.RAISED));
		String [] buttonNames = {"LoadCD", "AllCD", "Search", "Upload","Logoff","Exit" };
		for( int i = 0; i<buttonNames.length; i++ ) {
			JPanel buttonCont = new JPanel( new BorderLayout());
			buttonCont.setMaximumSize(new Dimension(100,40));
			JButton actionButton = new JButton(buttonNames[i]);
			actionButton.addActionListener(this);
			buttonCont.add( actionButton,BorderLayout.NORTH); 
			add(buttonCont);
		}
		setVisible(true);
	}
	
	public void actionPerformed( ActionEvent ae ) {
		String action = ae.getActionCommand();
		Object storedObject = DBOperation.getHelperBean().getStoredObject();
		DBOperation.getHelperBean().hideContainer();
		if( "Exit".equalsIgnoreCase(action)) {
			System.exit(0);
		}else if( "AllCD".equalsIgnoreCase(action)) {
			AvailableCDs availCd = null;
			if( storedObject!= null && storedObject instanceof AvailableCDs ) {
				availCd = (AvailableCDs) storedObject;
			} else {
				availCd = new AvailableCDs();
			}
			DBOperation.getHelperBean().addContainer(availCd);
		} else if( "Search".equalsIgnoreCase(action )) {
			SearchPane searchPane = null;
			if(storedObject!= null && storedObject instanceof SearchPane ) {
				searchPane = (SearchPane) storedObject;
			} else {
				searchPane = new SearchPane();
			}
			DBOperation.getHelperBean().addContainer(searchPane);
		} else if( "Upload".equalsIgnoreCase(action)) {
			FileUpload upload = null;
			if(storedObject!= null && storedObject instanceof FileUpload ) {
				upload = (FileUpload) storedObject;
			} else {
				upload = new FileUpload();
			}
			DBOperation.getHelperBean().addContainer(upload);
		} else if( "LoadCD".equalsIgnoreCase(action )) {
			LoadCDData loadPane = null;
			if(storedObject!= null && storedObject instanceof LoadCDData) {
				loadPane = (LoadCDData) storedObject;
			} else {
				loadPane = new LoadCDData();
			}
			DBOperation.getHelperBean().addContainer(loadPane);
		} else if( "Logoff".equalsIgnoreCase(action)) {
			MainWindow.logoff();
		}
	}

}
